package com.ia.aistream.manager.framework.filter.impl;

import com.ia.aistream.manager.authen.conf.AuthenConf;
import com.ia.aistream.manager.framework.filter.BaseTokenFilter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;


/**
 * Bearer Token 权限拦截器
 *
 * @author: chenjunsheng
 */
@Slf4j
public class BearerHeaderTokenFilter extends BaseTokenFilter {
    /**
     * 令牌前缀
     */
    private static final String TOKEN_PREFIX = "Bearer";
    @Autowired
    private AuthenConf authenConf;


    @Override
    protected String getToken(HttpServletRequest request) {
        AuthenConf.Token tokenConf = authenConf.getToken();
        String headerName = tokenConf.getHeader();
        String token = request.getHeader(headerName);
        if (StringUtils.isNotEmpty(token) && token.startsWith(TOKEN_PREFIX)) {
            return token.replace(TOKEN_PREFIX, "").trim();
        }
        return token;
    }
}
